import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;

import javax.tv.graphics.TVContainer;
import javax.tv.service.SIException;
import javax.tv.service.SIManager;
import javax.tv.service.SIRequestFailureType;
import javax.tv.service.SIRequestor;
import javax.tv.service.SIRetrievable;
import javax.tv.service.Service;
import javax.tv.service.guide.ProgramEvent;
import javax.tv.service.navigation.ServiceDetails;
import javax.tv.service.navigation.ServiceIterator;
import javax.tv.service.navigation.ServiceList;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;

public class SvcDispXlet implements Xlet, ActionListener {

    private Container root_container = null;

    private Panel panel = null;

    private List slist = null;

    private List plist = null;

    private Button button = null;

    private SIManager si_manager = null;

    private Retriever retriever = new Retriever();

    // init method   
    public void initXlet(XletContext ctx) {
        root_container = TVContainer.getRootContainer(ctx);
        panel = new Panel();
        panel.setBackground(Color.black);
        root_container.setLayout(null);
        root_container.setBounds(0, 0, 400, 300);
        root_container.add(panel);
        panel.setBounds(0, 0, root_container.getSize().height, root_container.getSize().width - 50);
        panel.setLayout(new GridLayout(2, 1));
        slist = new List();
        slist.setBackground(Color.lightGray);
        slist.addActionListener(this);
        panel.add(slist);
        plist = new List();
        plist.setBackground(Color.lightGray);
        panel.add(plist);
        button = new Button("Refresh");
        button.setBackground(Color.darkGray);
        button.setForeground(Color.white);
        button.addActionListener(this);
        root_container.add(button);
        button.setBounds(0, root_container.getSize().width - 50, root_container.getSize().height, 50);
        root_container.validate();
        root_container.setVisible(true);
        si_manager = SIManager.createInstance();
    }

    // start method   
    // Do an initial update of the slist   
    public void startXlet() {
        panel.validate();
        updateList(slist);
    }

    // pause   
    public void pauseXlet() {}

    // destroy   
    public void destroyXlet(boolean unconditional) {}

    // called when the refresh button is pressed   
    // or service is selected in the list   
    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == button) {
            this.updateList(slist);
            plist.removeAll();
        } else if (evt.getSource() == slist) {
            this.updateList(plist);
        }
    }

    private void updateList(List list) {
        //Clear out the old list   
        list.removeAll();
        ServiceList collection = si_manager.filterServices(null);
        ServiceIterator si = collection.createServiceIterator();
        si.toEnd();
        if (list == slist) {
            while (si.hasPrevious()) {
                slist.addItem(si.previousService().getName(), 0);
            }
        } else {
            while (si.hasPrevious()) {
                Service s = si.previousService();
                if (slist.getSelectedItem().equals(s.getName())) {
                    retriever.getPrograms(s);
                    break;
                }
            }
        }
    }

    class Retriever implements SIRequestor {

        DateFormat dfmt = DateFormat.getDateInstance(DateFormat.SHORT);

        DateFormat tfmt = DateFormat.getTimeInstance(DateFormat.SHORT);

        void getPrograms(Service s) {
            try {
                SIManager.createInstance().retrieveServiceDetails(s.getLocator(), this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void notifySuccess(SIRetrievable[] result) {
            if (result[0] instanceof ServiceDetails) {
                try {
                    ((ServiceDetails) result[0]).getProgramSchedule().retrieveFutureProgramEvents(new Date(), new Date(System.currentTimeMillis() + 600000000), this);
                } catch (SIException e) {}
            } else if (result[0] instanceof ProgramEvent) {
                for (int i = 0; i < result.length; i++) {
                    ProgramEvent e = (ProgramEvent) result[i];
                    plist.addItem(e.getName() + " : " + dfmt.format(e.getStartTime()) + " " + tfmt.format(e.getStartTime()));
                }
            }
        }

        public void notifyFailure(SIRequestFailureType reason) {}
    } //End Retriever class   
} //End SvcDispXlet class  